package com.cx.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.lang.model.element.NestingKind;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Date;


/**
 * @ClassName: BackUps
 * @Description: 数据库备份
 * @Author: cx
 * @Date: 2024/1/31 14:51
 **/
public class BackUps {

    //"\"C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysqldump\" -u "

    /**
     * @return void
     * @Description // 备份sql文件,在windows系统中
     * @Param1 mysqlDump文件地址
     * @Param2 数据库用户名
     * @Param3 数据库密码
     * @Param4 数据库名
     * @Param5 文件保存地址
     * @Date 2024/1/31 16:44
     * @Author cx
     **/
    public static void backupDB(String dbUserName, String dbPassword, String dbName, String backupPath) {
        String command = "\"C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysqldump\" -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + backupPath;

        String output = null;
        Process p = null;
        try {

            p = Runtime.getRuntime().exec(command);
            // reads the outputs
            InputStreamReader irs = new InputStreamReader(p.getErrorStream());
            BufferedReader br = new BufferedReader(irs);


            while ((output = br.readLine()) != null)
                System.out.println(output);

            // Wait for the process to finish
            int exitCode = p.waitFor();
            if (exitCode != 0) {
                throw new Exception("mysqldump process exited with error code: " + exitCode);
            }


            File backupFile = new File(backupPath);
            if (!backupFile.exists() || backupFile.length() == 0) {
                throw new Exception("数据库备份失败，备份文件为空!");
            } else {
                System.out.println("数据库备份成功!=============="+TimeTask.dateToString(new Date()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * @return void
     * @Description // 备份sql文件,在liunx系统中
     * @Param1 mysqlDump文件地址
     * @Param2 数据库用户名
     * @Param3 数据库密码
     * @Param4 数据库名
     * @Param5 文件保存地址
     * @Date 2024/1/31 16:44
     * @Author cx
     **/
    public static void backupDB2(String dbUserName, String dbPassword, String dbName, String backupPath) {
        String command = "/usr/bin/mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + backupPath;

        String output = null;
        Process p = null;
        try {
            //如果不存在该文件将会进行创建
//            File backupDir = new File(backupPath).getParentFile();
//            if (!backupDir.exists()) {
//                boolean dirsCreated = backupDir.mkdirs();
//                if (!dirsCreated) {
//                    System.out.println("文件不存在");
//                    throw new Exception("Failed to create directories: " + backupDir.getAbsolutePath());
//                }
//            }
            p = Runtime.getRuntime().exec(command);
            InputStreamReader irs = new InputStreamReader(p.getErrorStream());
            BufferedReader br = new BufferedReader(irs);

            while ((output = br.readLine()) != null)
                System.out.println(output);

            int exitCode = p.waitFor();
            if (exitCode != 0) {
                System.out.println("mysqldump process exited with error code");
                throw new Exception("mysqldump process exited with error code: " + exitCode);
            }

            File backupFile = new File(backupPath);
            if (!backupFile.exists() || backupFile.length() <153600) {
                throw new Exception("数据库备份失败，备份文件可能少于150kB,可能受到损坏或者被入侵");
            } else {
                System.out.println("数据库备份成功!=============="+TimeTask.dateToString(new Date()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("备份失败，错误信息：" + e.getMessage());
        }
    }
}
